package com.gupaoedu.pattern.prototype.deep;

import com.gupaoedu.pattern.prototype.Course;
import com.gupaoedu.pattern.prototype.Prototype;

import java.io.*;
import java.util.List;

/**
 * 2019/5/12
 * created by chenpp
 */
public class DeepStudent implements Cloneable ,Serializable{
    private String studentName;
    private List<Course> courses;//学生的课程表

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }
    /**
     * 深克隆,对于引用类型会创建新的对象
     * */
    public Object clone() {
        DeepStudent stu = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            oos.flush();
            oos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            stu  = (DeepStudent) ois.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return stu;
    }
}
